package com.fmdj.bff.driver.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.fmdj.bff.driver.controller.form.*;
import com.fmdj.bff.driver.service.DriverService;
import com.fmdj.common.exception.GlobalException;
import com.fmdj.common.util.CommonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/driver")
@Tag(name = "DriverController", description = "司机端模块接口")
@Slf4j
public class DriverController { //这个模块的controller与跟子项目的controller调用逻辑一致 开头都一样只是调用的路径会有区别

    @Autowired
    private DriverService driverService;

    @PostMapping("registerNewDriver") //前端的路径是什么,这里就是什么,这里是接受前端发来的数据,最后再返回给前端
    @Operation(summary = "注册司机接口")
    public CommonResult registerNewDriver(@RequestBody @Valid RegisterDriverForm form) {
        //向service层发送数据
        Long driverId = driverService.registerNewDriver(form);

        //利用sa-token设置token
        StpUtil.login(driverId);

        //获取token
        String token = StpUtil.getTokenInfo().getTokenValue();

        //最后返回前端所需参数token
        return CommonResult.ok().put(CommonResult.RETURN_TOKEN, token);
    }

    @PostMapping("/updateDriverAuth")
    @Operation(summary = "更新司机实名认证信息接口")
    public CommonResult updateDriverAuth(@RequestBody @Valid UpdateDriverAuthForm form) {

        long driverId = StpUtil.getLoginIdAsLong();

        form.setDriverId(driverId);

        int result = driverService.updateDriverAuth(form);

        return CommonResult.ok().put(CommonResult.RETURN_RESULT, result);
    }

    @PostMapping("/login")
    @Operation(summary = "司机登录")
    public CommonResult login(@RequestBody @Valid LoginForm form) {
        HashMap loginResult = driverService.login(form);

        if (loginResult == null) {
            throw new GlobalException("loginResult不能为空");
        }

        /**
         * archive：是否在腾讯云归档存放司机面部信息
         * realAuth：1未认证，2已认证，3审核中
         * */

        //获取司机id,认证状态,是否被注销
        Long driverId = MapUtil.getLong(loginResult, "id");
        byte realAuth = Byte.parseByte(MapUtil.getStr(loginResult, "realAuth"));
        Boolean archive = MapUtil.getBool(loginResult, "archive");

        //设置token并获取token
        StpUtil.login(driverId);
        String tokenValue = StpUtil.getTokenInfo().getTokenValue();

        return CommonResult.ok().put(CommonResult.RETURN_TOKEN, tokenValue).put("realAuth", realAuth).put("archive", archive);
    }

    //退出登录
    @GetMapping("/logout")
    @Operation(summary = "退出登录")
    public CommonResult logout() {
        StpUtil.logout();
        return CommonResult.ok();
    }

    @PostMapping("/selectDriverInfo")
    @Operation(summary = "查询司机个人信息")
    public CommonResult selectDriverInfo() {
        long driverId = StpUtil.getLoginIdAsLong();

        SelectDriverInfoForm selectDriverInfoForm = new SelectDriverInfoForm();
        selectDriverInfoForm.setDriverId(driverId);

        HashMap hashMap = driverService.selectDriverInfo(selectDriverInfoForm);

        return CommonResult.ok().put(CommonResult.RETURN_RESULT, hashMap);
    }

    @PostMapping("/selectWorkbenchData")
    @Operation(summary = "查询司机工作台数据")
    public CommonResult selectWorkbenchData() {
        /*根据你登陆时存储的stpUtil.login(driverId)*/
        long driverId = StpUtil.getLoginIdAsLong();

        //拿到司机的工作台数据(司机当日订单信息以及司机设置信息)
        HashMap result = driverService.selectWorkbenchData(driverId);

        return CommonResult.ok().put(CommonResult.RETURN_RESULT, result);
    }

    @GetMapping("/searchDriverAuth")
    @Operation(summary = "查询司机实名认证信息")
    public CommonResult searchDriverAuth() {

        long driverId = StpUtil.getLoginIdAsLong();

        SelectDriverAuthForm selectDriverAuthForm = new SelectDriverAuthForm();

        selectDriverAuthForm.setDriverId(driverId);

        HashMap result = driverService.selectDriverInfoById(selectDriverAuthForm);

        return CommonResult.ok().put(CommonResult.RETURN_RESULT, result);
    }

}
